package DAO;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BookValidateServlet
 */
public class BookValidateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private BookDAO bookDao=new BookDAO();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookValidateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookName=new String(request.getParameter("bookName").getBytes("ISO8859-1"));
		System.out.println("bookName"+bookName);
		if(bookName==null ||"".equals(bookName)) {
			bookName="书名不能为空！";
		}else {
			int count=bookDao.getDoubleNameCount(bookName);
			if(count>0) bookName="存在同名书籍，请重新输入！";
		}
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.getWriter().write("bookName");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String bookName=request.getParameter("bookName").trim();
		System.out.println("bookName"+bookName);
		if(bookName==null ||"".equals(bookName)) {
			bookName="书名不能为空！";
		}else {
			int count=bookDao.getDoubleNameCount(bookName);
			if(count>0) bookName="存在同名书籍，请重新输入！";
		}
		doGet(request, response);
	}

}
